import { Context } from "koa";
const { getInfoById } = require('../service/notice.service')
const { invalidNoticeId, NoticeIdisEmpty, verifyIdError } = require('../constants/err.type')

const noticeIdExist = async(ctx: Context, next: () => Promise<void>) => {
    try {
        const id = ctx.params.id
        if(!id) {
            return ctx.app.emit('error', NoticeIdisEmpty, ctx)
        }
        const notice: any = await getInfoById(id);
        if(!notice) {
            return ctx.app.emit('error', invalidNoticeId, ctx)
        }
    } catch (error) {
        console.error(error)
        return ctx.app.emit('error', verifyIdError, ctx)
    }
    await next()
}

module.exports = {
    noticeIdExist
}